package com.an.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.an.blog.admin.domain.bo.BlogArticleTagBo;
import com.an.blog.admin.domain.vo.BlogArticleTagVo;
import com.an.blog.admin.domain.BlogArticleTag;
import com.an.blog.mapper.BlogArticleTagMapper;
import com.an.blog.service.IBlogArticleTagService;

import java.util.List;
import java.util.Map;

/**
 * 文章标签中间Service业务层处理
 *
 * @author Ann
 * @date 2023-05-08
 */
@RequiredArgsConstructor
@Service
public class BlogArticleTagServiceImpl implements IBlogArticleTagService {

    private final BlogArticleTagMapper baseMapper;


    /**
     * 查询文章标签中间列表
     *
     * @param bo 业务对象
     * @return {@link List }<{@link BlogArticleTagVo }>
     * @author: Ann
     * @createDate: 2023/05/09 16:06:34
     * @version: 1.0.0
     */
    @Override
    public List<BlogArticleTagVo> queryList(BlogArticleTagBo bo) {
        LambdaQueryWrapper<BlogArticleTag> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BlogArticleTag> buildQueryWrapper(BlogArticleTagBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlogArticleTag> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getArticleId() != null, BlogArticleTag::getArticleId, bo.getArticleId());
        lqw.eq(bo.getTagId() != null, BlogArticleTag::getTagId, bo.getTagId());
        return lqw;
    }


    /**
     * 批量插入
     *
     * @param articleTagList 文章标记列表
     * @return {@link Boolean }
     * @author: Ann
     * @createDate: 2023/05/09 14:17:29
     * @version: 1.0.0
     */
    @Override
    public Boolean insertBatch(List<BlogArticleTag> articleTagList) {
        return baseMapper.insertBatch(articleTagList);
    }

    @Override
    public Boolean deleteByArticleId(Long articleId) {
        return baseMapper.delete(new LambdaQueryWrapper<BlogArticleTag>()
            .eq(BlogArticleTag::getArticleId, articleId)) > 0;
    }


}
